package com.taxinfo.view.dataImport;

import com.taxinfo.component.progress.TableProgressIndicator;
import com.taxinfo.component.table.ConditionTable;
import com.taxinfo.pojo.dto.MainReportDto;
import com.taxinfo.service.MainReportService;
import com.taxinfo.utils.SpringUtil;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.scene.control.Label;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class DataImportTable extends ConditionTable<MainReportDto> {

    private static final String MENU_CODE = "dataImport";

    private final MainReportService mainReportService = (MainReportService) SpringUtil.getBean("MainReportServiceImpl");
    private final ObservableList<MainReportDto> items = getItems();


    public DataImportTable(List<MainReportDto> items) {
        super(MENU_CODE, items, MainReportDto.class);
        this.items.addAll(items);
        showAllInfos();
    }


    /**
     * 显示所有会员信息
     */
    public void showAllInfos() {
        clearData();
        setPlaceholder(new TableProgressIndicator());

        Task<List<MainReportDto>> task = new Task<List<MainReportDto>>() {
            @Override
            protected List<MainReportDto> call() {
                List<MainReportDto> beans = mainReportService.selectAllData();
                if (beans != null)
                    return beans;
                else
                    return new ArrayList<>(0);
            }
        };
        new Thread(task).start();
        task.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue.size() != 0)
                items.addAll(newValue);
            else
                setPlaceholder(new Label("没有数据"));
        });
    }

    /**
     * 添加用户信息
     *
     * @param dto 用户信息
     */
    public void addBean(MainReportDto dto) {
        items.add(dto);
    }
}